package com.example.lzy.listviewsample.ExpandableListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lzy.listviewsample.R;

import java.util.List;

/**
 * Created by lzy on 2018/4/10.
 */

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context mContext = null;
    private List<String> mGroupList = null;
    private List<List<String>> mItemList = null;

    public MyExpandableListViewAdapter (Context context,List<String> groupList,List<List<String>> itemList){
        this.mContext = context;
        this.mGroupList = groupList;
        this.mItemList = itemList;
    }


    @Override
    public int getGroupCount() {
        return mGroupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return mItemList.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return mGroupList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return mItemList.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        GroupHolder groupHolder = null;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.expendlist_group,null);
            groupHolder = new GroupHolder();
            groupHolder.textView = (TextView) view.findViewById(R.id.groupname_tv);
            groupHolder.imageView = (ImageView) view.findViewById(R.id.group_img);
            view.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) view.getTag();

        }
        if (b){
            groupHolder.imageView.setImageResource(R.drawable.close);
        }else {
            groupHolder.imageView.setImageResource(R.drawable.open);
        }
        groupHolder.textView.setText(mGroupList.get(i));
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ItemHolder itemHolder = null;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.expendlist_item,null);
            itemHolder = new ItemHolder();
            itemHolder.textView1 = (TextView) view.findViewById(R.id.name);
            itemHolder.imageView1 = (ImageView) view.findViewById(R.id.icon_img);
            view.setTag(itemHolder);

        }else {
            itemHolder = (ItemHolder) view.getTag();
        }
        itemHolder.textView1.setText(mItemList.get(i).get(i1));
        itemHolder.imageView1.setImageResource(R.mipmap.ic_launcher);

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class GroupHolder{
        public TextView textView;
        public ImageView imageView;
    }

    class ItemHolder{
        public ImageView imageView1;
        public TextView textView1;
    }


}
